<?php session_start();?>

<?php

include "Vw_ticketsearch.php";
include "Vw_ticketsearchDao.php";
include "Datasource.php";

$searchBox = $_POST["searchBox"];
$searchCriteria = $_POST["searchCriteria"];
$$tempTicket = new Vw_ticketsearch();
$Vw_ticketsearchDao = new Vw_ticketsearchDao();
$conn = new Datasource("localhost","theanykey","root","");

switch ($searchCriteria)
{
    case "TicketID":
        $$tempTicket->setTicketID($searchBox);
        break;
    case "EmployeeLastName":
        $$tempTicket->setEmployeeLastName($searchBox);
        break;
    case "EmployeeFirstName":
        $$tempTicket->setEmployeeFirstName($searchBox);
        break;
    case "CustomerLastName":
        $$tempTicket->setCustomerLastName($searchBox);
        break;
    case "CustomerFirstName":
        $$tempTicket->setCustomerFirstName($searchBox);
        break;
    case "CustomerPhoneNumber":
        $$tempTicket->setCustomerPhoneNumber($searchBox);
        break;
}

$results = $Vw_ticketsearchDao->searchMatching($conn, $$tempTicket);

?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="maintenancePagesCSS.css" />
        <title></title>
    </head>
        <center>
          <form method="post" action="TicketSearchFunc.php" id="Vw_ticketsearchSearchForm">
            <center>
              <p><img src="images/TitleTicketSearch.gif" alt="Ticket Search" width="514" height="67"></p>
              <table width="785" class="SelectTable">
                <tbody>
                  <tr>
                    <td width="448"><input type="text" name="searchBox" value="<?php print($searchBox)?>" size="25" id="searchBox"/></td>
                    <td width="325"><select name="searchCriteria" id="searchCriteria">
                        <option value="TicketID">Ticket ID</option>
                        <option value="EmployeeLastName">Employee Last Name</option>
                        <option value="EmployeeFirstName">Employee First Name</option>
                        <option value="CustomerLastName">Customer Last Name</option>
                        <option value="CustomerFirstName">Customer First Name</option>
                        <option value="CustomerPhoneNumber">Customer Phone Number</option>
                      </select>
                    </td>
                  </tr>
                  <tr>
                    <td><input name="button" type="button" onClick = checkForm(); value="Search" /></td>
                    <td></td>
                  </tr>
                </tbody>
              </table>
            </center>
            <?php
        // put your code here
        if($searchBox != "")
        {
        print("<center><h3>Search Results</h3>");
        print("<table class='MenuDiv2' cellspacing='3'>
                    <thead>
                        <tr>
                            <th></th>
                            <th></th>
                            <th>Ticket ID</th>
                            <th>Customer ID</th>
                            <th>Current Emp Assigned</th>
                            <th>Expected Completion</th>
                            <th>Employee Last Name</th>
                            <th>Employee First Name</th>
                            <th>Employee Login ID</th>
                            <th>Employee Role</th>
                            <th>Customer Last Name</th>
                            <th>Customer First Name</th>
                            <th>Customer Phone Number</th>
                        </tr>
                    </thead>
                    <tbody>");

        foreach ($results as $temp)
        {
            print("<tr align='center'>
                        <td><a href = 'TicketUpdate.php?ticketID=".$temp->getTicketID()."'>Edit</a></td>
                        <td>");
            if($_SESSION['EMPROLE'] == 'Admin')
            {
                 print("<a href='' onclick='window.showModalDialog(\"TicketDelete.php?ticketID=".$temp->getTicketID()."\", null, \"dialogHeight:300px;dialogWidth:500px;dialogLeft:300;dialogTop:200\");'>Delete</a>");
            }
            print("</td><td>".$temp->getTicketID()."</td>
                        <td>".$temp->getCustomerID()."</td>
                        <td>".$temp->getCurrentEmpAssigned()."</td>
                        <td>".$temp->getExpectCmpltDate()."</td>
                        <td>".$temp->getEmployeeLastName()."</td>
                        <td>".$temp->getEmployeeFirstName()."</td>
                        <td>".$temp->getEmployeeLoginID()."</td>
                        <td>".$temp->getEmployeeRole()."</td>
                        <td>".$temp->getCustomerLastName()."</td>
                        <td>".$temp->getCustomerFirstName()."</td>
                        <td>".$temp->getCustomerPhoneNumber()."</td>
                  </tr>");
        }

        print("</tbody></table></center>");

        }

        ?>
            <BR>
            <BR>
            <script src="links.js" language="javascript" type="text/javascript"></script>
            <script type="text/javascript">
            writeTicketLinks("", "");
                      </script>
          </form>


        </center>
        </body>
    <script type="text/javascript">
        function checkForm()
        {
            if((document.getElementById("searchBox").value == ""))
            {
                alert("Search field is blank.");
            }
            else
            {
                document.getElementById("Vw_ticketsearchSearchForm").submit();
            }
        }
    </script>
</html>